{
 "cells": [
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "# 索引与切片\n",
    "- 基本索引：通过整数索引直接访问元素，索引是从0开始的\n",
    "- 行/列切片：使用冒号：切片语法选择行或列的子集\n",
    "- 连续切片：从起始索引到结束索引，按照一定步长进行切片\n",
    "- 使用slice函数：通过slice(start,stop,step)定义切片规则\n",
    "- 布尔索引：通过布尔条件帅选满足条件的元素。支持逻辑运算符&、｜"
   ],
   "id": "1758736fd74763b8"
  },
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-09-04T13:54:14.158209Z",
     "start_time": "2025-09-04T13:54:14.149096Z"
    }
   },
   "source": [
    "# 一维数组的索引与切片\n",
    "import numpy as np\n",
    "\n",
    "arr = np.random.randint(1,100,20) # 创建一个20个元素的数组,[1.13模块编程.12package,100)随机整数\n",
    "print(arr)\n",
    "print(arr[10])\n",
    "print(arr[0:10]) # 获取索引0到9的元素[0,10),共计10个元素\n",
    "\n",
    "print(arr[::2]) # 获取索引0到19的元素，每隔2个取一个[0,19),共计10个元素\n",
    "\n",
    "print(arr[2:5]) # 获取索引2到4的元素[2,5),共计3个元素\n",
    "\n",
    "print(arr[arr > 10]) # 支持布尔条件筛选满足条件的元素\n",
    "print(arr[(arr > 10) & (arr < 50)])\n",
    "print(arr[(arr > 10) | (arr < 50)])"
   ],
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[45 41 37 84 39  5 60 46 29 13 25 83  3 34 57 16 55 82 67 98]\n",
      "25\n",
      "[45 41 37 84 39  5 60 46 29 13]\n",
      "[45 37 39 60 29 25  3 57 55 67]\n",
      "[37 84 39]\n",
      "[45 41 37 84 39 60 46 29 13 25 83 34 57 16 55 82 67 98]\n",
      "[45 41 37 39 46 29 13 25 34 16]\n",
      "[45 41 37 84 39  5 60 46 29 13 25 83  3 34 57 16 55 82 67 98]\n"
     ]
    }
   ],
   "execution_count": 10
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-04T13:55:43.791160Z",
     "start_time": "2025-09-04T13:55:43.784903Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# slice 函数\n",
    "arr = np.random.randint(1,100,20)\n",
    "print(arr)\n",
    "print(arr[slice(0,10)])\n",
    "print(arr[:10])\n",
    "print(arr[slice(0,10,3)])"
   ],
   "id": "b3ce8586f7d22878",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[15 20  7 79 93 48 73 17 88 38 21 23 47 25 45 61  4 17 85 43]\n",
      "[15 20  7 79 93 48 73 17 88 38]\n",
      "[15 20  7 79 93 48 73 17 88 38]\n",
      "[15 79 73 38]\n"
     ]
    }
   ],
   "execution_count": 13
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-04T13:57:46.234984Z",
     "start_time": "2025-09-04T13:57:46.227095Z"
    }
   },
   "cell_type": "code",
   "source": [
    "arr = np.random.randint(1,100,(4,8))\n",
    "print(arr)"
   ],
   "id": "865ebbe822b4156f",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[29 30 61  8  3 79 64 69]\n",
      " [97 92 44 38 44 66 66 25]\n",
      " [85 13  8  7 25 41 82 97]\n",
      " [27 46 96 35 61 18 21 64]]\n"
     ]
    }
   ],
   "execution_count": 14
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-04T14:05:59.352526Z",
     "start_time": "2025-09-04T14:05:59.343161Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 二维数组索引\n",
    "print(arr[1,3])\n",
    "print(arr[:,:])\n",
    "print(arr[:,0:3])\n",
    "print(arr[1,2:5]) # 获取索引1行，索引2到4列的元素\n",
    "\n",
    "print(arr[arr > 50]) # 支持布尔条件筛选满足条件的元素,返回形状一维数组\n",
    "\n",
    "print(arr[2,:])\n",
    "print(arr[2,:][arr[2,:] > 50])\n",
    "\n",
    "print(arr[:,3])\n",
    "print(arr[:,3][arr[:,3] > 50])"
   ],
   "id": "3f7e52f860768384",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "38\n",
      "[[29 30 61  8  3 79 64 69]\n",
      " [97 92 44 38 44 66 66 25]\n",
      " [85 13  8  7 25 41 82 97]\n",
      " [27 46 96 35 61 18 21 64]]\n",
      "[[29 30 61]\n",
      " [97 92 44]\n",
      " [85 13  8]\n",
      " [27 46 96]]\n",
      "[44 38 44]\n",
      "[61 79 64 69 97 92 66 66 85 82 97 96 61 64]\n",
      "[85 13  8  7 25 41 82 97]\n",
      "[85 82 97]\n",
      "[ 8 38  7 35]\n",
      "[]\n"
     ]
    }
   ],
   "execution_count": 31
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
